import type {
  ComponentEventContext,
  CreateCrudOptionsProps,
  CreateCrudOptionsRet,
} from '@fast-crud/fast-crud';

import { ref } from 'vue';

import { DefGenProjectApi } from '#/api/devOperation/developer/defGenProject';
import { ProjectTypeEnum } from '#/enums/base';
import { DictEnum } from '#/enums/commonEnum';
import { backendDict, indexColumn } from '#/plugins/fast-crud/common';

export function createCrudOptions(
  props: CreateCrudOptionsProps,
): CreateCrudOptionsRet {
  const selectedIds = ref([] as string[]);

  const onSelectionChange = (changed: string[]) => {
    selectedIds.value = changed;
  };

  return {
    crudOptions: {
      container: {
        is: 'fs-layout-card',
      },
      request: {
        addRequest: DefGenProjectApi.addRequest,
      },
      table: {
        striped: true,
        rowKey: (row: any) => row.id,
        checkedRowKeys: selectedIds,
        'onUpdate:checkedRowKeys': onSelectionChange,
      },
      form: {
        mode: 'add',
        group: {
          type: 'tabs', // collapse, tabs
          // type: 'collapse', // collapse, tabs
          accordion: false, // 手风琴模式
          displayDirective: 'show',
          triggerAreas: ['arrow'],
          groups: {
            cloud: {
              header: 'lamp-cloud/lamp-boot 配置',
              columns: [
                'type',
                'outputDir',
                'author',
                'description',
                'projectPrefix',
                'serviceName',
                'parent',
                'moduleName',
                'groupId',
                'version',
                'serverPort',
                'seata',
              ],
            },
            util: {
              header: 'lamp-util 配置',
              columns: ['utilParent', 'utilGroupId'],
            },
          },
        },
      },
      columns: {
        ...indexColumn(props.crudExpose),
        type: {
          title: '项目类型',
          type: 'dict-radio',
          dict: backendDict({ type: DictEnum.ProjectTypeEnum }),
          form: {
            value: ProjectTypeEnum.CLOUD,
            component: {
              on: {
                onClick(e: ComponentEventContext) {
                  props.context?.changeTab(e.key);
                },
              },
            },
          },
        },
        outputDir: {
          title: '生成路径',
          type: 'text',
          form: {
            component: {
              on: {
                onFocus(e: ComponentEventContext) {
                  props.context?.changeTab(e.key);
                },
              },
            },
          },
        },
        author: {
          title: '作者',
          type: 'text',
          form: {
            component: {
              on: {
                onFocus(e: ComponentEventContext) {
                  props.context?.changeTab(e.key);
                },
              },
            },
          },
        },
        description: {
          title: '中文服务名',
          type: 'text',
          form: {
            component: {
              on: {
                onFocus(e: ComponentEventContext) {
                  props.context?.changeTab(e.key);
                },
              },
            },
          },
        },
        projectPrefix: {
          title: '项目前缀',
          type: 'text',
          form: {
            helper: { text: '如： lamp-base、lamp-base-server 中的lamp' },
            component: {
              on: {
                onFocus(e: ComponentEventContext) {
                  props.context?.changeTab(e.key);
                },
              },
            },
          },
        },
        serviceName: {
          title: '服务名',
          type: 'text',
          form: {
            helper: {
              text: '如： lamp-base-server 中的base\n如： lamp-system-server system',
            },
            component: {
              on: {
                onFocus(e: ComponentEventContext) {
                  props.context?.changeTab(e.key);
                },
              },
            },
          },
        },
        parent: {
          title: '基础包',
          type: 'text',
          form: {
            helper: {
              text: 'lamp-cloud或lamp-boot项目的基础包\n如： top.tangyh.lamp.base.dao.common 包中的 top.tangyh.lamp',
            },
            component: {
              on: {
                onFocus(e: ComponentEventContext) {
                  props.context?.changeTab(e.key);
                },
                onInput(e: ComponentEventContext) {
                  e.form.groupId = e.form.parent;
                },
              },
            },
          },
        },
        moduleName: {
          title: '模块名',
          type: 'text',
          form: {
            helper: {
              text: '如： top.tangyh.lamp.base.dao.common 包中的 base',
            },
            component: {
              on: {
                onFocus(e: ComponentEventContext) {
                  props.context?.changeTab(e.key);
                },
              },
            },
          },
        },
        groupId: {
          title: 'groupId',
          type: 'text',
          form: {
            helper: { text: 'lamp-cloud或lamp-boot项目 pom文件的groupId' },
            component: {
              on: {
                onFocus(e: ComponentEventContext) {
                  props.context?.changeTab(e.key);
                },
              },
            },
          },
        },
        version: {
          title: '版本号',
          type: 'text',
          form: {
            component: {
              on: {
                onFocus(e: ComponentEventContext) {
                  props.context?.changeTab(e.key);
                },
              },
            },
          },
        },
        serverPort: {
          title: '端口号',
          type: 'number',
          form: {
            component: {
              on: {
                onFocus(e: ComponentEventContext) {
                  props.context?.changeTab(e.key);
                },
              },
            },
          },
        },
        seata: {
          title: '集成Seata',
          type: 'text',
          form: {
            component: {
              name: 'a-switch',
              vModel: 'checked',
              checkedValue: true,
              uncheckedValue: false,
            },
          },
        },
        utilParent: {
          title: 'utilParent',
          type: 'text',
          form: {
            component: {
              on: {
                onFocus(e: ComponentEventContext) {
                  props.context?.changeTab(e.key);
                },
                onInput(e: ComponentEventContext) {
                  e.form.utilGroupId = e.form.utilParent;
                },
              },
            },
          },
        },
        utilGroupId: {
          title: 'utilGroupId',
          type: 'text',
          form: {
            component: {
              on: {
                onFocus(e: ComponentEventContext) {
                  props.context?.changeTab(e.key);
                },
              },
            },
          },
        },
      },
    },
  };
}

export const frontRules = {
  addForm: {
    rules: {},
  },
  editForm: {
    rules: {},
  },
};
